package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.utils.MyLog;
import com.hyphenate.exceptions.HyphenateException;

public class EaseChatRowText extends EaseChatRow{
    private TextView online;
    private TextView callPhone;
	private TextView contentView;
    private LinearLayout onlineContainer;
    private TextView hintService;
    public EaseChatRowText(Context context, EMMessage message, int position, BaseAdapter adapter) {
		super(context, message, position, adapter);
    }

	@Override
	protected void onInflateView() {
		inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
				R.layout.ease_row_received_message : R.layout.ease_row_sent_message, this);


	}

	@Override
	protected void onFindViewById() {
		contentView = (TextView) findViewById(R.id.tv_chatcontent);
		onlineContainer = (LinearLayout) findViewById(R.id.ll_onlinecontainer);
        callPhone = (TextView) findViewById(R.id.tv_call);
        online = (TextView)findViewById(R.id.tv_online);
        hintService = (TextView)findViewById(R.id.tv_service_hint);
	}

    @Override
    public void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, BufferType.SPANNABLE);


        //TODO　设置人工在线转换按钮
       int service = message.getIntAttribute(EaseChatFragment.ATTR_KEY_RECEIVE,EaseChatFragment.ATTR_VALUE_ONLINE);
        if(EaseChatFragment.ATTR_VALUE_ANDROID == service){
           //TODO 隐藏
            onlineContainer.setVisibility(GONE);
            if(isAndroid){
                online.setText("人工在线");
            }else{
                online.setText("机器人");
            }
        }else{
            onlineContainer.setVisibility(GONE);
        }
        // 设置提示
        if(message.getIntAttribute(EaseChatFragment.ATTR_KEY_HINT,EaseChatFragment.ATTR_VALUE_HINT_HIDE) == EaseChatFragment.ATTR_VALUE_HINT_SHOW){
            hintService.setVisibility(VISIBLE);
            if(message.getIntAttribute(EaseChatFragment.ATTR_KEY_HINT_TYPE,EaseChatFragment.ATTR_VALUE_HINT_TYPE_ONLINE) == EaseChatFragment.ATTR_VALUE_HINT_TYPE_ONLINE){
                hintService.setText("小乐为你服务");
            }else{
                hintService.setText("亮亮机器人为你服务");
            }
        }else{
            hintService.setVisibility(GONE);
        }


        handleTextMessage();
    }

    protected void handleTextMessage() {
        //TODO 展示发送状态

        if (message.direct() == EMMessage.Direct.SEND) {
            if(message.getIntAttribute(EaseChatFragment.ATTR_KEY_SEND_TO,EaseChatFragment.ATTR_VALUE_ONLINE)==(EaseChatFragment.ATTR_VALUE_ANDROID)){//机器人不展示
                progressBar.setVisibility(View.GONE);
                statusView.setVisibility(View.GONE);
            }else{
                setMessageSendCallback();
                switch (message.status()) {
                    case CREATE:
                        progressBar.setVisibility(View.GONE);
                        statusView.setVisibility(View.VISIBLE);
                        break;
                    case SUCCESS:
                        progressBar.setVisibility(View.GONE);
                        statusView.setVisibility(View.GONE);
                        break;
                    case FAIL:
                        progressBar.setVisibility(View.GONE);
                        statusView.setVisibility(View.VISIBLE);
                        break;
                    case INPROGRESS:
                        progressBar.setVisibility(View.VISIBLE);
                        statusView.setVisibility(View.GONE);
                        break;
                    default:
                        break;
                }

            }


        }else{
            if(!message.isAcked() && message.getChatType() == ChatType.Chat){
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void setClickListener() {
        super.setClickListener();

        if(online != null){
            online.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(itemClickListener != null){
                        itemClickListener.onOnlineClick();
                        online.setText("机器人");
                    }
                }
            });
        }
        if(callPhone != null){
            callPhone.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(itemClickListener != null){
                        itemClickListener.onCallPhoneClick();
                    }
                }
            });
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO 条目点击事件
        MyLog.d(TAG,"条目点击事件  消息内容："+message.getBody().toString());
        
    }



}
